<?
class Combo
{
var $label;
var $index;
var $value;
var $description;
#..........EventHandlers...........
var $onblur;
var $onchange;

function Combo ()
{
$this->label = "Select an option: ";
$this->index = -1;
$this->value = "";
$this->description = "";

}

function AddItem ($value, $description= "")
{
	if($description != ""){
		$this->index++;
		$this->value[$this->index] = $value;
		$this->description[$this->index] = $description;
	}else{
		$this->index++;
		$this->value[$this->index] = $value;
		$this->description[$this->index] = $value;
	}
}

function Clear()
{
$this->Combo();
}


function FillDB($qry, $value="")
{
	$rows = DataBase::ExecuteQuery($qry);
	foreach($rows as $key => $row)
		$this->AddItem ($row[0],$row[1]);	
}

function Fill($results){
	foreach($results as $row)
		$this->AddItem($row[0], $row[1]);
}

	function Render ($name, $selected="", $style = "", $disabled="",$submit = "", $change="")
	{
	if ($this->index<0)
	{
	echo "";
	}
	else
	{
	echo "<select id=\"$name\" name=\"$name\" $style ";
	if ($submit!="") echo " OnSubmit=\"$submit\" ";   // subnit por submit
	if ($disabled!="") echo $disabled; 
	if ($change!="") echo "onchange=\"$change\"";
	if (isset($this->onchange)) echo "onchange=\"$this->onchange\"";	
	if (isset($this->onblur)) echo "onblur=\"$this->onblur\"";
	echo ">\r"; 
	for ($x=0;$x<$this->index+1;$x++)
	{
		if($selected == $this->value[$x])
			echo "<option  selected=\"selected\" value=\"" . $this->value[$x] . "\">" . $this->description[$x] . "</option>\r";
		else
			echo "<option value=\"" . $this->value[$x] . "\">" . $this->description[$x] . "</option>\r";
	}
	echo "</select>\r";
	}
	}#End Funtion Display
	
#----------------------------------
function RenderNoSpaces($name, $selected="", $style = "", $disabled="",$submit = "", $change="")
	{
	if ($this->index<0)
	{
	echo "";
	}
	else
	{
	echo "<select id=\"$name\" name=\"$name\" $style ";
	if ($submit!="") echo " OnSubmit=\"$submit\" ";   // subnit por submit
	if ($disabled!="") echo $disabled; 
	if ($change!="") echo "onchange=\"$change\"";
	if (isset($this->onchange)) echo "onchange=\"$this->onchange\"";	
	if (isset($this->onblur)) echo "onblur=\"$this->onblur\"";
	echo ">"; 
	for ($x=0;$x<$this->index+1;$x++)
	{
		if($selected == $this->value[$x])
			echo "<option  selected=\"selected\" value=\"" . $this->value[$x] . "\">" . $this->description[$x] . "</option>";
		else
			echo "<option value=\"" . $this->value[$x] . "\">" . $this->description[$x] . "</option>";
	}
	echo "</select>";
	}
	}#End Funtion Display
#----------------------------------
	
	
	function RenderOptionGroupDB($name, $onChange = ""){
	#esta es una implementacion apresurada se recomienda mejorarla

	$result = mysql_query("SELECT * FROM rubros_categorias ORDER BY Categoria ASC");
	$i =0;
	$categorias = array();
		while($row = mysql_fetch_array($result)){
			$categorias[$i] = $row;
			$i++;
		}
		
	echo "<select name=\"$name\" ";
	if ($onChange!="") echo "onchange=\"$onChange\"";
	echo ">\n"; 
	echo "<option value=\"0\">Todos los Rubros</option>";
		#obteniendo los rubros
		foreach($categorias as $key => $value){
			$sql = "SELECT * FROM rubros WHERE id_categoria = ". $value[id]. " ORDER BY rubro ASC";
			$result = mysql_query($sql);
			echo "<optgroup label= \"$value[categoria]\" style=\"font-style:normal;\">\n";
			while($row = mysql_fetch_array($result)){
				echo "\t<option value=\"$row[id]\">$row[rubro]</option>\n";
			}
			echo "</optgroup>\n";
		}#end Foreach
		$sql = "SELECT * FROM rubros WHERE id_categoria = 0 ORDER BY rubro ASC";
		$result = mysql_query($sql);
		while($row = mysql_fetch_array($result)){
			echo "<option value=\"$row[id]\">$row[rubro]</option>\n";
		}
	echo "</select>";
	}#end displayOptionGroupDB
	
	function fill_sequence($min, $max){
	for($i=$min;$i<=$max;$i++)
		$this->addItem($i,$i);
	}

}// End Class
?>